package com.qixiaobao.direct.settle.service.impl;

import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.direct.settle.domain.dto.DirectSettleDTO;
import com.qixiaobao.direct.settle.domain.vo.DirectSettledVO;
import com.qixiaobao.direct.settle.domain.vo.DirectWaitPayVO;
import com.qixiaobao.direct.settle.domain.vo.DirectWaitSettleVO;
import com.qixiaobao.direct.settle.mapper.DirectSettleMapper;
import com.qixiaobao.direct.settle.service.IDirectSettleService;
import com.qixiaobao.system.domain.Dict;
import com.qixiaobao.system.domain.Person;
import com.qixiaobao.system.domain.SlaveUser;
import com.qixiaobao.system.domain.dto.PersonDTO;
import com.qixiaobao.system.mapper.DictMapper;
import com.qixiaobao.system.mapper.PersonMapper;
import com.qixiaobao.system.mapper.SlaveUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 直投结算管理  业务层实现
 * @Author yangxm
 * @Date 2025/6/26 14:31
 */
@Slf4j
@Service
public class DirectSettleServiceImpl implements IDirectSettleService {

    @Autowired
    private DirectSettleMapper directSettleMapper;
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private SlaveUserMapper slaveUserMapper;
    @Autowired
    private PersonMapper personMapper;


    /**
     * @Description 查询 待结算列表
     * @Author yangxm
     * @Date 2025/6/26 14:33
     * @param directSettleDTO
     * @return java.util.List<com.qixiaobao.direct.domain.vo.DirectWaitSettleVO>
     */
    @Override
    public List<DirectWaitSettleVO> selectWaitSettlementList(DirectSettleDTO directSettleDTO) {
        log.info("查询 直投结算管理-待结算列表， 参数：{}", directSettleDTO);
        long begin = System.currentTimeMillis();
        List<DirectWaitSettleVO> list = directSettleMapper.selectWaitSettlementList(directSettleDTO);
        log.info("查询 直投结算管理-待结算sql， 耗时：{}ms", System.currentTimeMillis() - begin);
        if (StringUtils.isNotEmpty(list)){

            //字典
            List<Dict> dictList = dictMapper.selectDictListByType("insureperiod");
            Map<String, String> insureperiodMap = dictList.stream()
                    .filter(c -> c.getVal() != null)
                    .collect(Collectors.toMap(Dict::getK, Dict::getVal));

            //用户信息
            Map<Long, String> usernameMap = slaveUserMapper.selectUsernameList().stream()
                    .collect(Collectors.toMap(SlaveUser::getId, SlaveUser::getUsername));

            DirectWaitSettleVO directWaitSettlementVO;
            for (int i = 0; i < list.size(); i++) {
                directWaitSettlementVO = list.get(i);
                directWaitSettlementVO.setIndex(i + 1);
                directWaitSettlementVO.setInsurePeriodVal(insureperiodMap.get(directWaitSettlementVO.getInsurePeriod()));
                directWaitSettlementVO.setServiceName(usernameMap.get(directWaitSettlementVO.getServiceId()));
                directWaitSettlementVO.setFinanceReckonVal(StringUtils.equals("0", directWaitSettlementVO.getFinanceReckon()) ? "否" : "是");
                directWaitSettlementVO.setPayCompanyName(directWaitSettlementVO.getCustomerName());
                directWaitSettlementVO.setSettlePerson("增员:" + directWaitSettlementVO.getAddNum() + ";减员:" + directWaitSettlementVO.getMinusNum());
            }
        }
        log.info("查询 直投结算管理-待结算列表，总耗时：{}ms", System.currentTimeMillis() - begin);
        return list;
    }

    /**
     * @Description 查询待结算人员列表
     * @Author yangxm
     * @Date 2025/6/26 14:33
     * @param personDTO
     * @return java.util.List<com.qixiaobao.nondirect.invoice.domain.domain.Person>
     */
    @Override
    public List<Person> selectWaitSettlePersonList(PersonDTO personDTO) {
        return personMapper.selectChargePersonList(personDTO);
    }

    /**
     * @Description 待支付 列表
     * @Author yangxm
     * @Date 2025/6/26 16:14
     * @param directSettleDTO
     * @return java.util.List<com.qixiaobao.direct.domain.vo.DirectWaitPayVO>
     */
    @Override
    public List<DirectWaitPayVO> selectWaitPayList(DirectSettleDTO directSettleDTO) {
        log.info("查询 直投结算管理-待支付列表， 参数：{}", directSettleDTO);
        long begin = System.currentTimeMillis();
        List<DirectWaitPayVO> list = directSettleMapper.selectWaitPayList(directSettleDTO);
        if (StringUtils.isNotEmpty(list)){

            //用户信息
            Map<Long, String> usernameMap = slaveUserMapper.selectUsernameList().stream()
                    .collect(Collectors.toMap(SlaveUser::getId, SlaveUser::getUsername));

            DirectWaitPayVO item;
            for (int i = 0; i < list.size(); i++) {
                item = list.get(i);
                item.setIndex(i + 1);
                item.setServiceName(usernameMap.get(item.getServiceId()));
                item.setFinanceReckonVal(StringUtils.equals("0", item.getFinanceReckon()) ? "否" : "是");
                item.setSettlePerson("增员:" + item.getAddNum() + ";减员:" + item.getMinusNum());
            }
        }
        log.info("查询 直投结算管理-待支付列表，总耗时：{}ms", System.currentTimeMillis() - begin);
        return list;
    }

    /**
     * @Description 已结算列表
     * @Author yangxm
     * @Date 2025/6/26 16:15
     * @param directSettleDTO
     * @return java.util.List<com.qixiaobao.direct.domain.vo.DirectSettledVO>
     */
    @Override
    public List<DirectSettledVO> selectSettledList(DirectSettleDTO directSettleDTO) {
        log.info("查询 直投结算管理-已结算列表， 参数：{}", directSettleDTO);
        long begin = System.currentTimeMillis();
        List<DirectSettledVO> list = directSettleMapper.selectSettledList(directSettleDTO);
        return null;
    }
}
